<template>
	<view>
		<view>
			<view style="padding: 40rpx;">
				<uni-forms class="form" ref="studentLeaveForm" :modelValue="studentLeaveForm" label-position="left"
					labelWidth="120px">
					<uni-forms-item label="班级" required name="classId">
						<uni-data-select v-model="studentLeaveForm.classId" :localdata="classList"
							@change="selectClass"></uni-data-select>
					</uni-forms-item>
					<uni-forms-item label="学生" required name="studentNo">
						<button type="default" size="mini" @click="showDrawer('showLeftCC')"><text
								class="word-btn-white">选择学生</text>
						</button>
						<uni-drawer style="padding: 40rpx 10rpx 100rpx 10rpx;" ref="showLeftCC" mode="left" :width="120"
							@change="change($event,'showLeftCC')">
							<scroll-view class="scrollHeight" :scroll-top="0" scroll-y="true" style="height: 100vh;">
								<view style="padding:60rpx 10rpx 250rpx 10rpx;" class="">
									<uni-data-checkbox :wrap="true" multiple v-model="studentLeaveForm.studentNo"
										:localdata="studentList"></uni-data-checkbox>
								</view>
							</scroll-view>
						</uni-drawer>

					</uni-forms-item>
					<uni-forms-item label="已选择学生" name="">
						<button size="mini" v-for="(item,index) in studentNameList" :key="index">{{item}}</button>
					</uni-forms-item>
					<uni-forms-item label="开始日期" required name="leaveStartTime">
						<uni-datetime-picker type="date" :clear-icon="false" @change="stratDays" :start="Date.now()"
							placeholder="选择请假开始日期" v-model="studentLeaveForm.leaveStartTime" />
					</uni-forms-item>
					<uni-forms-item label="结束日期" required name="leaveEndTime">
						<uni-datetime-picker type="date" :clear-icon="false" @change="cacleDays" :start="Date.now()"
							placeholder="选择请假结束日期" v-model="studentLeaveForm.leaveEndTime" />
					</uni-forms-item>
					<uni-forms-item label="请假天数" required name="leaveTotalTime">
						<!-- <uni-number-box v-model="studentLeaveForm.leaveTotalTime" :min="1" /> -->
						<uni-easyinput type="text" v-model="studentLeaveForm.leaveTotalTime" disabled />
					</uni-forms-item>
					<uni-forms-item label="请假类型" required name="leaveType">
						<uni-data-select v-model="studentLeaveForm.leaveType" :localdata="leaveTypeList"
							field="dictValue as value, dictLabel as name"></uni-data-select>
					</uni-forms-item>
					<uni-forms-item label="请假原因" required name="leaveRemarks">
						<uni-easyinput type="textarea" v-model="studentLeaveForm.leaveRemarks" placeholder="请输入请假原因" />
					</uni-forms-item>
				</uni-forms>
			</view>
			<button @click="submit" type="primary">提交请假</button>
			<view @click="leaveHistory()">
				<button style="background-color: #FF0088; margin-top: 20rpx; color: #fff;">请假历史记录</button>
			</view>
		</view>
	</view>
</template>

<script>
	import teachClass from '@/api/modules/teachClass'
import teacher from '@/api/modules/teacher';
	export default {
		data() {
			return {
				userNam: '',
				topName: '宿舍请假',
				studentLeaveForm: {
					classId: "",
					studentNo: "",
					leaveStartTime: "",
					leaveEndTime: "",
					leaveTotalTime: "",
					leaveType: "",
					leaveRemarks: "",
				},
				leaveTypeList: [],
				studentList: [],
				classList: [],
				rules: {
					classId: {
						rules: [{
							required: true,
							errorMessage: '班级不能为空'
						}]
					},
					studentNo: {
						rules: [{
							required: true,
							errorMessage: '学生不能为空'
						}]
					},
					leaveStartTime: {
						rules: [{
							required: true,
							errorMessage: '开始日期不能为空'
						}]
					},
					leaveEndTime: {
						rules: [{
							required: true,
							errorMessage: '结束日期不能为空'
						}]
					},
					leaveTotalTime: {
						rules: [{
							required: true,
							errorMessage: '请假天数不能为空'
						}]
					},
					leaveType: {
						rules: [{
							required: true,
							errorMessage: '请假类型不能为空'
						}]
					},
					leaveRemarks: {
						rules: [{
							required: true,
							errorMessage: '请假原因不能为空'
						}]
					},
				},
				studentNameList: [],
				studentNoArray: [],
				studentNameArray: [],
				startDateArray: [],
				phoneArray: [],
				leaveId: [],
				leaveDays: [],
				endDateArray: [],
				leaveId: [],
				leaveDays: [],
				allStudentList: [],
				className: "",
				leaveReason: [],
				leaveType: [],
				startMinutes: "",
				userName: "",
			}
		},
		onReady() {
			this.$refs.studentLeaveForm.setRules(this.rules);
		},
		methods: {
			leaveHistory() {
				uni.navigateTo({
					url: '/pages/leave/leaveHistoryList'
				})
			},
			// 获取班级列表
			getClassData() {
				this.$api.teacher.tecClassList(this.loginId).then(res => {
					console.log(res)
					res.data.forEach(item => {
						let obj = {}
						obj.value = item.classId
						obj.text = item.className
						this.classList.push(obj)
					})

				})
			},
			// 班级学生
			getClassStuListByCName(className) {
				this.$api.teacher.getClassStuListByCName(className).then(res => {
					console.log(res)
					this.allStudentList = res.data
					res.data.forEach(item => {

						let obj = {}
						obj.text = item.studentName
						obj.value = item.studentNo
						this.studentList.push(obj)
					})
				})
			},
			// 选择班级
			selectClass(e) {
				if (!e) {
					this.studentList = []
				}
				this.studentList = []
				this.className = ""
				this.classList.forEach(item => {
					if (item.value == e) {
						this.className = item.text
						this.getClassStuListByCName(this.className)
					}
				})

			},
			

			// 提交数据
			submit() {
				
				var myDate = new Date();
				var mytime = myDate.getHours(); //获取当前时间
				if (mytime > 21) {
					console.log(mytime > 22, mytime < 22, mytime, "time")
					uni.showToast({
						title: '当前时间不能提交',
						icon: "none"
					})
					return;
				}
				if (!this.studentLeaveForm.classId) {
					uni.showToast({
						title: "请选择班级",
						icon: 'none',
					})
					return
				}
				console.log("this.studentNameList.length", this.studentNameList.length)
				if (this.studentNameList.length <= 0) {
					uni.showToast({
						title: "请选择学生",
						icon: 'none',
					})
					return
				}
				if (!this.studentLeaveForm.leaveStartTime) {
					uni.showToast({
						title: "请填写开始日期",
						icon: 'none',
					})
					return
				}
				if (!this.studentLeaveForm.leaveEndTime) {
					uni.showToast({
						title: "请填写结束日期",
						icon: 'none',
					})
					return
				}
				if (!this.studentLeaveForm.leaveType) {
					uni.showToast({
						title: "请填写请假类型",
						icon: 'none',
					})
					return
				}
				if (!this.studentLeaveForm.leaveRemarks) {
					uni.showToast({
						title: "请填写请假原因",
						icon: 'none',
					})
					return
				}
				console.log(999999)
				// this.$refs.studentLeaveForm.validate((err, formData) => {
				// 	if (!err) {
				console.log(666)
				this.studentLeaveForm.studentNo.forEach(item => {
					this.studentList.forEach(e => {
						if (item == e.value) {
							this.studentNameArray.push(e.text)
						}
					})
					this.startDateArray.push(this.studentLeaveForm.leaveStartTime)
					this.endDateArray.push(this.studentLeaveForm.leaveEndTime)
					this.leaveId.push(0)
					this.leaveDays.push(this.studentLeaveForm.leaveTotalTime)
					this.leaveType.push(this.studentLeaveForm.leaveType)
					this.leaveReason.push(this.studentLeaveForm.leaveRemarks)
				})
				this.studentLeaveForm.studentNo.forEach(item => {
					this.allStudentList.forEach(e => {
						if (item == e.studentNo) {
							this.phoneArray.push(e.phone)
						}
					})
				})
				console.log({
					classId: this.studentLeaveForm.classId,
					className: this.className,
					endDateArray: this.endDateArray,
					leaveDays: this.leaveDays,
					leaveId: this.leaveId,
					phoneArray: this.phoneArray,
					startDateArray: this.startDateArray,
					studentNameArray: this.studentNameArray,
					studentNoArray: this.studentLeaveForm.studentNo,
					leaveTypeArray: this.leaveType,
					leaveReasonArray: this.leaveReason,
				})
				// console.log(this.className)
				teachClass.createUpdateStuLeaves({
					classId: this.studentLeaveForm.classId,
					className: this.className,
					endDateArray: this.endDateArray,
					leaveDays: this.leaveDays,
					leaveId: this.leaveId,
					phoneArray: this.phoneArray,
					startDateArray: this.startDateArray,
					studentNameArray: this.studentNameArray,
					studentNoArray: this.studentLeaveForm.studentNo,
					leaveTypeArray: this.leaveType,
					leaveReasonArray: this.leaveReason,
				}).then(res => {
					uni.showToast({
						icon: "none",
						title: "提交成功"
					})
					
					
					
					this.studentLeaveForm = {
						classId: "",
						studentNo: "",
						leaveStartTime: "",
						leaveEndTime: "",
						leaveTotalTime: "",
						leaveType: "",
						leaveRemarks: "",
					}
					this.studentList = []
					this.studentNameList = []
					this.studentNoArray = []
					this.studentNameArray = []
					this.startDateArray = []
					this.phoneArray = []
					this.leaveId = []
					this.leaveDays = []
					this.endDateArray = []
					this.leaveId = []
					this.leaveDays = []
					this.allStudentList = []
					this.className = ""
					this.leaveReason = []
					this.leaveType = []
				}).catch(error => {
					uni.showToast({
						icon: "none",
						title: error.data.message
					})
					this.studentLeaveForm = {
						classId: "",
						studentNo: "",
						leaveStartTime: "",
						leaveEndTime: "",
						leaveTotalTime: "",
						leaveType: "",
						leaveRemarks: "",
					}
					this.studentList = []
					this.studentNameList = []
					this.studentNoArray = []
					this.studentNameArray = []
					this.startDateArray = []
					this.phoneArray = []
					this.leaveId = []
					this.leaveDays = []
					this.endDateArray = []
					this.leaveId = []
					this.leaveDays = []
					this.allStudentList = []
					this.className = ""
					this.leaveReason = []
					this.leaveType = []
				})



				// 	}
				// })
			},

			showDrawer(e) {
				if (!this.studentLeaveForm.classId) {
					uni.showToast({
						icon: "none",
						title: "请先选择班级"
					})
					return
				}
				this.$refs[e].open()
			},

			// 抽屉状态发生变化触发
			change(e, type) {
				this.studentNameList = []
				if (!e) {
					this.studentList.forEach(item => {
						this.studentLeaveForm.studentNo.forEach(e => {
							if (e == item.value) {
								this.studentNameList.push(item.text)
							}
						})
					})
				}
			},

			cacleDays(e) {
				if (this.studentLeaveForm.leaveEndTime && this.studentLeaveForm.leaveStartTime) {
					this.studentLeaveForm.leaveTotalTime = (new Date(this.studentLeaveForm.leaveEndTime).getTime() -
						new Date(
							this.studentLeaveForm.leaveStartTime).getTime()) / 1000 / 60 / 60 / 24 + 1
				}
				console.log(e)
			},
			stratDays(e) {
				if (this.studentLeaveForm.leaveEndTime && this.studentLeaveForm.leaveStartTime) {
					this.studentLeaveForm.leaveTotalTime = (new Date(this.studentLeaveForm.leaveEndTime).getTime() -
						new Date(
							this.studentLeaveForm.leaveStartTime).getTime()) / 1000 / 60 / 60 / 24 + 1
				}
				console.log(e)
			},
			getLeaveType() {
				teachClass.getDictByType("leave_type").then(res => {
					res.data.forEach(item => {
						let obj = {}
						obj.text = item.dictLabel
						obj.value = item.dictValue
						this.leaveTypeList.push(obj)
					})
				})
			}

		},
		onLoad() {
			this.userName = uni.getStorageSync("userName")
			this.loginId = uni.getStorageSync("loginId")
			console.log(this.loginId)
			this.getClassData()
			this.getLeaveType()
		},
	}
</script>

<style>
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	.indexContent {
		height: 100vh;
	}

	.form {
		padding: 20rpx;
	}

	/deep/.uni-drawer__content--visible[data-v-558f1882] {
		padding-bottom: 50rpx;
	}
</style>